package com.zn.web.rpc;

import com.zn.util.HttpClientUtil;
import com.zn.vo.Response;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author YinJiaWei
 * @version 2018/8/25 20:12
 */
@Component
public class MdRemoteCall {

    private static final String ADDRESS_IP="http://103.45.14.235";
    private static final String PROT=":8086";
    private static final String SINAJS_IP="http://hq.sinajs.cn";

    /**
     * 查询标的证券信息 每天一次
     * @return
     */
    public Response target(){
        Response response=HttpClientUtil.get(ADDRESS_IP+PROT,
                HttpClientUtil.Builder.builder("/target")
        );
        return response;
    }

    /**
     * 查询指定标的合约日期列表，每天只需要查询一次
     * @return
     */
    public Response date(Integer target){
        Response response=HttpClientUtil.get(ADDRESS_IP+PROT,
                HttpClientUtil.Builder.builder("/date")
                        .with("tg", target)
        );
        return response;
        /**
         * {
         "date":[
         {"ym":"201810"},
         {"ym":"201811"},
         {"ym":"201812"},
         {"ym":"201903"}
         ]
         }
         */
    }
    /**
     * 查询指定标的指定日期的合约行权价格列表
     */
    public Response exec(Integer tg,String date){
        Response response=HttpClientUtil.get(ADDRESS_IP+PROT,
                HttpClientUtil.Builder.builder("/exec")
                        .with("tg", tg)
                        .with("date", date)
        );
        return response;
        /**
         * {
         "exec":[
         {"price":"2.2500"},
         {"price":"2.3000"},
         {"price":"2.3500"},
         {"price":"2.4000"},
         {"price":"2.4500"},
         {"price":"2.5000"},
         {"price":"2.5500"},
         {"price":"2.6000"},
         {"price":"2.6500"},
         {"price":"2.7000"},
         {"price":"2.7500"},
         {"price":"2.8000"},
         {"price":"2.8500"}
         ]
         }
         */
    }
    /**
     * 查询合约基础信息
     */
    public Response stat(Integer tg,String date){
        Response response=HttpClientUtil.get(ADDRESS_IP+PROT,
                HttpClientUtil.Builder.builder("/stat")
                        .with("tg", tg)
                        .with("date", date)
        );
        return response;
        /**
         *{
         "stat":[{"id":"10001443","name":"50ETF购10月2300","lp":0.1483,"up":0.4308,"lw":0.0001,"pc":0.1831,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.3000,"ot":0,"pd":4,"ei":1,"di":0},
         {"id":"10001444","name":"50ETF购10月2350","lp":0.1036,"up":0.3846,"lw":0.0001,"pc":0.1369,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.3500,"ot":0,"pd":4,"ei":2,"di":0},
         {"id":"10001445","name":"50ETF购10月2400","lp":0.0677,"up":0.3417,"lw":0.0001,"pc":0.0940,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.4000,"ot":0,"pd":4,"ei":3,"di":0},
         {"id":"10001446","name":"50ETF购10月2450","lp":0.0419,"up":0.3075,"lw":0.0001,"pc":0.0598,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.4500,"ot":0,"pd":4,"ei":4,"di":0},
         {"id":"10001447","name":"50ETF购10月2500","lp":0.0255,"up":0.2815,"lw":0.0001,"pc":0.0361,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.5000,"ot":0,"pd":4,"ei":5,"di":0},
         {"id":"10001448","name":"50ETF购10月2550","lp":0.0140,"up":0.2599,"lw":0.0001,"pc":0.0195,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.5500,"ot":0,"pd":4,"ei":6,"di":0},
         {"id":"10001449","name":"50ETF购10月2600","lp":0.0074,"up":0.2451,"lw":0.0001,"pc":0.0097,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.6000,"ot":0,"pd":4,"ei":7,"di":0},
         {"id":"10001450","name":"50ETF购10月2650","lp":0.0035,"up":0.2343,"lw":0.0001,"pc":0.0039,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.6500,"ot":0,"pd":4,"ei":8,"di":0},
         {"id":"10001451","name":"50ETF购10月2700","lp":0.0015,"up":0.2272,"lw":0.0001,"pc":0.0018,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.7000,"ot":0,"pd":4,"ei":9,"di":0},
         {"id":"10001452","name":"50ETF沽10月2300","lp":0.0038,"up":0.2144,"lw":0.0001,"pc":0.0021,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.3000,"ot":1,"pd":4,"ei":1,"di":0},
         {"id":"10001453","name":"50ETF沽10月2350","lp":0.0098,"up":0.2280,"lw":0.0001,"pc":0.0057,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.3500,"ot":1,"pd":4,"ei":2,"di":0},
         {"id":"10001454","name":"50ETF沽10月2400","lp":0.0241,"up":0.2469,"lw":0.0001,"pc":0.0146,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.4000,"ot":1,"pd":4,"ei":3,"di":0},
         {"id":"10001455","name":"50ETF沽10月2450","lp":0.0468,"up":0.2721,"lw":0.0001,"pc":0.0298,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.4500,"ot":1,"pd":4,"ei":4,"di":0},
         {"id":"10001456","name":"50ETF沽10月2500","lp":0.0804,"up":0.3020,"lw":0.0001,"pc":0.0543,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.5000,"ot":1,"pd":4,"ei":5,"di":0},
         {"id":"10001457","name":"50ETF沽10月2550","lp":0.1194,"up":0.3353,"lw":0.0001,"pc":0.0876,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.5500,"ot":1,"pd":4,"ei":6,"di":0},
         {"id":"10001458","name":"50ETF沽10月2600","lp":0.1633,"up":0.3757,"lw":0.0001,"pc":0.1280,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.6000,"ot":1,"pd":4,"ei":7,"di":0},
         {"id":"10001459","name":"50ETF沽10月2650","lp":0.2097,"up":0.4212,"lw":0.0001,"pc":0.1735,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.6500,"ot":1,"pd":4,"ei":8,"di":0},
         {"id":"10001460","name":"50ETF沽10月2700","lp":0.2595,"up":0.4707,"lw":0.0001,"pc":0.2230,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.7000,"ot":1,"pd":4,"ei":9,"di":0},
         {"id":"10001461","name":"50ETF购10月2750","lp":0.0010,"up":0.2214,"lw":0.0001,"pc":0.0010,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.7500,"ot":0,"pd":4,"ei":10,"di":0},
         {"id":"10001462","name":"50ETF沽10月2750","lp":0.3074,"up":0.5207,"lw":0.0253,"pc":0.2730,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.7500,"ot":1,"pd":4,"ei":10,"di":0},
         {"id":"10001463","name":"50ETF购10月2250","lp":0.1934,"up":0.4755,"lw":0.0001,"pc":0.2278,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.2500,"ot":0,"pd":4,"ei":0,"di":0},
         {"id":"10001464","name":"50ETF沽10月2250","lp":0.0016,"up":0.2033,"lw":0.0001,"pc":0.0010,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.2500,"ot":1,"pd":4,"ei":0,"di":0},
         {"id":"10001465","name":"50ETF购10月2800","lp":0.0005,"up":0.2159,"lw":0.0001,"pc":0.0005,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.8000,"ot":0,"pd":4,"ei":11,"di":0},
         {"id":"10001466","name":"50ETF购10月2850","lp":0.0004,"up":0.2107,"lw":0.0001,"pc":0.0003,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.8500,"ot":0,"pd":4,"ei":12,"di":0},
         {"id":"10001467","name":"50ETF沽10月2800","lp":0.3562,"up":0.5707,"lw":0.0753,"pc":0.3230,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.8000,"ot":1,"pd":4,"ei":11,"di":0},
         {"id":"10001468","name":"50ETF沽10月2850","lp":0.4052,"up":0.6207,"lw":0.1253,"pc":0.3730,"pt":0.0001,"vm":10000.0000,"tg":0,"date":"201810","exec":2.8500,"ot":1,"pd":4,"ei":12,"di":0}
         ]
         }
         */
    }

    public Response mdInfo(Integer tg,String date){
        Response response=null;
        if(StringUtils.isBlank(date)){
            //查询所有的合约
            response=HttpClientUtil.get(ADDRESS_IP+PROT,
                    HttpClientUtil.Builder.builder("/md")
                            .with("tg", tg)
            );
        }else{
            //查询当月的合约行情
            response=HttpClientUtil.get(ADDRESS_IP+PROT,
                    HttpClientUtil.Builder.builder("/md")
                            .with("tg", tg)
                            .with("date", date)
            );
        }
        return response;
    }

    public String getEtfMdInfo(){
        String etfMdStr=HttpClientUtil.get(HttpClientUtil.Builder.builder("/list=s_sh510050"), SINAJS_IP);
        return etfMdStr;
    }

    public String getSinaMdInfo(String instrumentCode){
        String sinaTick=HttpClientUtil.get(HttpClientUtil.Builder.builder("/list=CON_OP_"+instrumentCode), SINAJS_IP);
        return sinaTick;
    }

    public Response getSinaRemainderDay(String date){
        /**
         * 传过来的date为 yyyymm
         * 将值转换为 yyyy-mm
         */
        date=date.substring(0,4)+"-"+date.substring(4,date.length());
        Response response=HttpClientUtil.get("http://stock.finance.sina.com.cn",
                HttpClientUtil.Builder.builder("/futures/api/openapi.php/StockOptionService.getRemainderDay")
                        .with("date", date)
        );
        return response;
    }
}
